
namespace Profit.ViewModels
{
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Interop;
    using Properties;
    using Resources;
    using Views.Interfaces;

    public enum FolderTypeEnum
    {
        Log,
        Agents,
        Fittings
    }

    public sealed class SettingsViewModel : ViewModelBase<ISettingsView>
    {
        private ICommand _resetCommand;
        private ICommand _selectFolderCommand;

        /// <summary>
        /// Creates a new instance
        /// </summary>
        /// <param name="view">The IView instance associated</param>
        public SettingsViewModel(ISettingsView view) : base(view)
        {
            view.Title = Messages.label_settings;
        }


        public ICommand ResetCommand
        {
            get { return _resetCommand ?? (_resetCommand = new BindableCommand<object>(ResetCommandHandler)); }
        }

        public ICommand SelectFolderCommand
        {
            get { return _selectFolderCommand ?? (_selectFolderCommand = new BindableCommand<FolderTypeEnum>(SelectFolderCommandHandler)); }
        }

        
        private static void ResetCommandHandler(object state)
        {
            Settings.Default.Reset();
        }

        internal override void OnValidate(object value)
        {
            base.OnValidate(value);
            Settings.Default.Save();
        }

        private void SelectFolderCommandHandler(FolderTypeEnum type)
        {
            var path = GetSelectedPath();
            if (string.IsNullOrEmpty(path)) return;

            switch (type)
            {
                case FolderTypeEnum.Log :
                    Settings.Default.PathLog = path;
                    break;
                case FolderTypeEnum.Agents :
                    Settings.Default.PathAgents = path;
                    break;
                case FolderTypeEnum.Fittings :
                    Settings.Default.PathFittings = path;
                    break;
            }
        }

        private string GetSelectedPath()
        {
            var helper = new WindowInteropHelper(((Window) View));
            return Win32Native.FolderBrowserDialog.SelectFolder(helper.Handle);
        }
    }
}